from http_client import HttpClient
from exceptions import RequestError, ResponseError
from pydantic import BaseModel
from typing import List

# 定义数据模型
class UserBase(BaseModel):
    username: str
    email: str

class UserCreate(UserBase):
    password: str

class UserResponse(UserBase):
    id: int

class UserList(BaseModel):
    items: List[UserResponse]
    total: int

def main():
    # 创建客户端实例
    client = HttpClient(
        base_url='http://localhost:8000',
        timeout=30,
        max_retries=3,
        # token='your_token_here'
    )
    
    try:
        # GET请求示例 - 使用数据模型验证响应
        response = client.get('/users', 
                            # data_model=UserList,
                            # params={'page': 1, 'size': 10}
                            )
        users = response.data
        print(f"用户: {users}")

        user1 = {"username": 11,
                 "password": None,
                 "email": "string"
                }

        res = client.post('/users',
                          json=user1
                          )
        print(f"result:{res.model_dump()},type:{type(res)}")


        # for user in users.items:
        #     print(f"用户: {user.username}, 邮箱: {user.email}")
        
        # # POST请求示例 - 使用数据模型验证请求和响应
        # user_data = UserCreate(
        #     username='test_user',
        #     email='test@example.com',
        #     password='secure_password'
        # )
        # response = client.post('/users', 
        #                      data_model=UserResponse,
        #                      json=user_data.model_dump())
        # new_user = response.data
        # print(f"创建用户: {new_user.username}, ID: {new_user.id}")
        
        # # PUT请求示例
        # update_data = UserBase(
        #     username='updated_user',
        #     email='new_email@example.com'
        # )
        # response = client.put(f'/users/{new_user.id}',
        #                     data_model=UserResponse,
        #                     json=update_data.model_dump())
        # updated_user = response.data
        # print(f"更新用户: {updated_user.username}")
        
        # # DELETE请求示例
        # response = client.delete(f'/users/{new_user.id}')
        # print(f"删除用户: {response.msg}")
        
    except RequestError as e:
        print(f"请求错误: {str(e)}")
    except ResponseError as e:
        print(f"响应错误: {str(e)}")

if __name__ == '__main__':
    main() 